package com.ttk.issuetracker.model;

import javax.json.JsonObjectBuilder;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;


/**
 * The persistent class for the task_log database table.
 * 
 */
@Entity
@Table(name="task_log")
@NamedQueries({
@NamedQuery(name = "TaskLog.findByUser", 
	query = "SELECT tl FROM TaskLog tl WHERE tl.user = :user AND tl.taskLogDate BETWEEN :startDate AND :endDate order by tl.taskLogDate ASC"),
@NamedQuery(name = "TaskLog.findTaskLogCountByTask", query = "SELECT count(tl) FROM TaskLog tl WHERE tl.task = :task "),
@NamedQuery(name = "TaskLog.findTaskLogCountByUser", query = "SELECT count(tl) FROM TaskLog tl WHERE tl.user = :user ")
})
public class TaskLog extends AbstractEntity implements EntityItem<Integer> {
	private static final long serialVersionUID = 1L;
	
	static final SimpleDateFormat DATE_FORMAT_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Basic(optional = false)
	@Column(name="id_task_log")
	private int idTaskLog;

	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 2000)
	@Column(name="task_description")
	private String taskDescription;

	@Basic(optional = false)
    @NotNull
	@Temporal(TemporalType.DATE)
	@Column(name="task_log_date")
	private Date taskLogDate;

	@Basic(optional = false)
    @NotNull
	@Column(name="task_minutes")
	private int taskMinutes;

	//bi-directional many-to-one association to Task
	@ManyToOne
	@JoinColumn(name="id_task")
	private Task task;

	//bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name="username")
	private User user;

	public TaskLog() {
	}

	public int getIdTaskLog() {
		return this.idTaskLog;
	}

	public void setIdTaskLog(int idTaskLog) {
		this.idTaskLog = idTaskLog;
	}

	public String getTaskDescription() {
		return this.taskDescription;
	}

	public void setTaskDescription(String taskDescription) {
		this.taskDescription = taskDescription;
	}

	public Date getTaskLogDate() {
		return this.taskLogDate;
	}

	public void setTaskLogDate(Date taskLogDate) {
		this.taskLogDate = taskLogDate;
	}

	public int getTaskMinutes() {
		return this.taskMinutes;
	}

	public void setTaskMinutes(int taskMinutes) {
		this.taskMinutes = taskMinutes;
	}

	public Task getTask() {
		return this.task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public Integer getId() {
		return idTaskLog;
	}

	@Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TaskLog other = (TaskLog) obj;
        return Objects.equals(this.idTaskLog, other.idTaskLog);
    }

    @Override
    public String toString() {
        return "TaskLog[ idTaskLog=" + idTaskLog + " ]";
    }

	@Override
	public void addJson(JsonObjectBuilder builder) {
		builder.add("idTaskLog", idTaskLog)
			.add("taskDescription", taskDescription)
			.add("taskLogDate", taskLogDate == null ? "" : DATE_FORMAT_yyyyMMdd.format(taskLogDate))
			.add("taskMinutes", taskMinutes);
		if (user != null) {
			user.addJson(builder);
		}
		if (task != null) {
			task.addJson(builder); 
		}
		
	}
}